<?php

namespace app\admin\service;

use app\admin\model\NoticeList;
use app\admin\model\UserNotice;
use app\common\validate\CommonValidate;
use think\exception\ValidateException;
use think\facade\Request;

class NoticeService
{
    public static function goEdit()
    {
        if (Request::isPost()) {
            $data = Request::post();
            //验证
            $rule = [
                'title' => 'require',
                'describe' => 'require',
                'content' => 'require',
            ];
            $message = [
                'title.require' => "消息标题不能为空",
                'describe.require' => "消息描述不能为空",
                'content.require' => "消息内容不能为空",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            unset($data['__token__']);
            try {
                if ($data['id']) {
                    UserNotice::update($data);
                } else {
                    $userIds = explode(',', $data['user_ids']);
                    if (in_array(0, $userIds)) $userIds = [0];
                    unset($data['id']);
                    foreach ($userIds as $userId) {
                        $data['user_id'] = $userId;
                        UserNotice::create($data);
                    }
                }
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goRemove($id)
    {
        $model = UserNotice::find($id);
        if ($model->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->delete();
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    public static function goEditBulletin()
    {
        if (Request::isPost()) {
            $data = Request::post();
            //验证
            $rule = [
                'content' => 'require',
            ];
            $message = [
                'content.require' => "公告内容不能为空",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            unset($data['__token__']);
            try {
                if ($data['id']) {
                    NoticeList::update($data);
                } else {
                    unset($data['id']);
                    NoticeList::create($data);
                }
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goRemoveBulletin($id)
    {
        $model = NoticeList::find($id);
        if ($model->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->delete();
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    public static function goStatusBulletin()
    {
        $data = Request::post();
        $model = NoticeList::find($data['id']);
        if ($model->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->save([
                'status' => $data['status'],
            ]);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }
}